import request from '@/utils/request';
import {AxiosPromise} from 'axios';
import {
    ConversationBindMaskForm,
    ConversationBindToolsForm,
    UserChatConfigForm,
    UserChatConversationDetailVO,
    UserChatConversationPageQuery,
    UserChatConversationVO,
    UserChatMaskVO,
    UserChatMessagePageQuery,
    UserChatMessageVO,
    UserChatToolPageQuery, UserChatToolProviderVO,
    UserChatToolVO
} from './type';

const API_BASE = '/ai/user/chat';

const API_SUFFIXES = {
    CONVERSATION_PAGE: '/conversations',
    DRAFT_CONVERSATION: '/drafts',
    MESSAGE_PAGE: '/messages',
    TOOL_PROVIDER_LIST: '/tool-provider',
    DETAIL: '/{conversationId}/detail',
    UPDATE_CONFIG: '/{conversationId}/config',
    AI_TITLE: '/{conversationId}/ai-title',
    MASK_LIST: '/masks',
    TOOL_PAGE: '/tools',
    UPDATE_TITLE: '/{conversationId}/title',
    DELETE_CONVERSATION: '/{conversationIds}',
    DELETE_MESSAGE: '/{messageId}/{conversationId}',
    BIND_MASK: '/bind-mask',
    BIND_TOOLS: '/bind-tools'
};

export class UserChatAPI {
    /**
     * 分页查询会话
     */
    static CONVERSATION_PAGE = {
        endpoint: `${API_BASE}${API_SUFFIXES.CONVERSATION_PAGE}`,
        permission: 'ai:user:chat',
        request: (query: UserChatConversationPageQuery): AxiosPromise<PageResult<UserChatConversationVO[]>> => {
            return request<PageResult<UserChatConversationVO[]>>({
                url: UserChatAPI.CONVERSATION_PAGE.endpoint,
                method: 'get',
                params: query
            });
        }
    };

    /**
     * 查询草稿会话
     */
    static DRAFT_CONVERSATION = {
        endpoint: `${API_BASE}${API_SUFFIXES.DRAFT_CONVERSATION}`,
        permission: 'ai:user:chat',
        request: (): AxiosPromise<string> => {
            return request<string>({
                url: UserChatAPI.DRAFT_CONVERSATION.endpoint,
                method: 'get',
            });
        }
    };

    /**
     * 分页查询消息
     */
    static MESSAGE_PAGE = {
        endpoint: `${API_BASE}${API_SUFFIXES.MESSAGE_PAGE}`,
        permission: 'ai:user:chat',
        request: (query: UserChatMessagePageQuery): AxiosPromise<PageResult<UserChatMessageVO[]>> => {
            return request<PageResult<UserChatMessageVO[]>>({
                url: UserChatAPI.MESSAGE_PAGE.endpoint,
                method: 'get',
                params: query
            });
        }
    };

    /**
     * 分页查询消息
     */
    static TOOL_PROVIDER_LIST = {
        endpoint: `${API_BASE}${API_SUFFIXES.TOOL_PROVIDER_LIST}`,
        permission: 'ai:user:chat',
        request: (): AxiosPromise<PageResult<UserChatToolProviderVO[]>> => {
            return request<PageResult<UserChatToolProviderVO[]>>({
                url: UserChatAPI.TOOL_PROVIDER_LIST.endpoint,
                method: 'get',
            });
        }
    };

    /**
     * 查询会话详情
     */
    static DETAIL = {
        endpoint: (conversationId: string) => `${API_BASE}${API_SUFFIXES.DETAIL.replace('{conversationId}', conversationId)}`,
        permission: 'ai:user:chat',
        request: (conversationId: string): AxiosPromise<UserChatConversationDetailVO> => {
            return request<UserChatConversationDetailVO>({
                url: UserChatAPI.DETAIL.endpoint(conversationId),
                method: 'get'
            });
        }
    };

    /**
     * 修改会话配置
     */
    static UPDATE_CONFIG = {
        endpoint: (conversationId: string) => `${API_BASE}${API_SUFFIXES.UPDATE_CONFIG.replace('{conversationId}', conversationId)}`,
        permission: 'ai:user:chat',
        request: (conversationId: string, form: UserChatConfigForm): AxiosPromise<void> => {
            return request<void>({
                url: UserChatAPI.UPDATE_CONFIG.endpoint(conversationId),
                method: 'put',
                data: form
            });
        }
    };

    /**
     * AI调整会话标题
     */
    static AI_TITLE = {
        endpoint: (conversationId: string) => `${API_BASE}${API_SUFFIXES.AI_TITLE.replace('{conversationId}', conversationId)}`,
        permission: 'ai:user:chat',
        request: (conversationId: string): AxiosPromise<string> => {
            return request<string>({
                url: UserChatAPI.AI_TITLE.endpoint(conversationId),
                method: 'put'
            });
        }
    };

    /**
     * 查询所有面具
     */
    static MASK_LIST = {
        endpoint: `${API_BASE}${API_SUFFIXES.MASK_LIST}`,
        permission: 'ai:user:chat',
        request: (maskName?: string): AxiosPromise<UserChatMaskVO[]> => {
            return request<UserChatMaskVO[]>({
                url: UserChatAPI.MASK_LIST.endpoint,
                method: 'get',
                params: maskName ? {maskName} : undefined
            });
        }
    };

    /**
     * 分页查询工具
     */
    static TOOL_PAGE = {
        endpoint: `${API_BASE}${API_SUFFIXES.TOOL_PAGE}`,
        permission: 'ai:user:chat',
        request: (query: UserChatToolPageQuery): AxiosPromise<PageResult<UserChatToolVO[]>> => {
            return request<PageResult<UserChatToolVO[]>>({
                url: UserChatAPI.TOOL_PAGE.endpoint,
                method: 'get',
                params: query
            });
        }
    };

    /**
     * 修改会话标题
     */
    static UPDATE_TITLE = {
        endpoint: (conversationId: string) => `${API_BASE}${API_SUFFIXES.UPDATE_TITLE.replace('{conversationId}', conversationId)}`,
        permission: 'ai:user:chat',
        request: (conversationId: string, title: string): AxiosPromise<void> => {
            return request<void>({
                url: UserChatAPI.UPDATE_TITLE.endpoint(conversationId),
                method: 'put',
                params: {title}
            });
        }
    };

    /**
     * 删除会话
     */
    static DELETE_CONVERSATION = {
        endpoint: (conversationIds: string[]) => `${API_BASE}${API_SUFFIXES.DELETE_CONVERSATION.replace('{conversationIds}', conversationIds.join(","))}`,
        permission: 'ai:user:chat',
        request: (conversationIds: string[]): AxiosPromise<void> => {
            return request<void>({
                url: UserChatAPI.DELETE_CONVERSATION.endpoint(conversationIds),
                method: 'delete'
            });
        }
    };

    /**
     * 删除会话消息
     */
    static DELETE_MESSAGE = {
        endpoint: (messageId: string, conversationId: string) => `${API_BASE}${API_SUFFIXES.DELETE_MESSAGE.replace('{messageId}', messageId).replace('{conversationId}', conversationId)}`,
        permission: 'ai:user:chat',
        request: (messageId: string, conversationId: string): AxiosPromise<void> => {
            return request<void>({
                url: UserChatAPI.DELETE_MESSAGE.endpoint(messageId, conversationId),
                method: 'delete'
            });
        }
    };

    /**
     * 会话绑定面具
     */
    static BIND_MASK = {
        endpoint: `${API_BASE}${API_SUFFIXES.BIND_MASK}`,
        permission: 'ai:user:chat',
        request: (form: ConversationBindMaskForm): AxiosPromise<void> => {
            return request<void>({
                url: UserChatAPI.BIND_MASK.endpoint,
                method: 'post',
                data: form
            });
        }
    };

    /**
     * 会话绑定工具集合
     */
    static BIND_TOOLS = {
        endpoint: `${API_BASE}${API_SUFFIXES.BIND_TOOLS}`,
        permission: 'ai:user:chat',
        request: (form: ConversationBindToolsForm): AxiosPromise<void> => {
            return request<void>({
                url: UserChatAPI.BIND_TOOLS.endpoint,
                method: 'post',
                data: form
            });
        }
    };
}